import contextMenu from './contextMenu.vue'
import {createApp} from 'vue'

const d = document.createElement('div')
d.style.cssText = `position:fixed;z-index:999;`
let app = null
function close() {
    d.remove()
    if(app) app.unmount()
    document.removeEventListener('click', close)
}

function show(e, menu) {
    if(app) app.unmount()
    app = createApp(contextMenu, {
        menu
    })
    app.mount(d)
    d.style.left = e.clientX + 'px'
    d.style.top = e.clientY + 'px'
    document.body.appendChild(d)
    document.addEventListener('click', close)
}

export function useContextMenu() {
    
    return {
        show
    }
}